package com.ssy.lingxi.product.controller.pc.customer;

import com.ssy.lingxi.common.constant.tree.Node;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.CustomerCategoryRequest;
import com.ssy.lingxi.product.api.model.request.SimpleIdListRequest;
import com.ssy.lingxi.product.api.model.request.SimpleIdRequest;
import com.ssy.lingxi.product.api.model.response.commodity.CustomerCategoryResponse;
import com.ssy.lingxi.product.entity.customer.CustomerAttribute;
import com.ssy.lingxi.product.entity.customer.CustomerAttributeValue;
import com.ssy.lingxi.product.entity.customer.CustomerCategory;
import com.ssy.lingxi.product.entity.platform.Category;
import com.ssy.lingxi.product.service.customer.ICustomerCategoryService;
import com.ssy.lingxi.product.service.platform.ICategoryService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @menu: 商品能力--品类管理
 * 商品能力--品类管理类
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/22
 */
@RestController
@RequestMapping("/product/customer")
public class CustomerCategoryController extends BaseController {

    @Resource
    private ICustomerCategoryService customerCategoryService;

    @Resource
    private ICategoryService categoryService;

    /**
     * 同步平台后台品类
     * @param simpleIdListRequest 平台后台品类id集合
     * @return
     */
    @RequestMapping(value = "/syncCategory", method = RequestMethod.POST)
    public Wrapper<Boolean> syncCategory(@RequestBody SimpleIdListRequest simpleIdListRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerCategoryService.syncCategory(sysUser, simpleIdListRequest));
    }

    /**
     * 查询品类树--当前登录用户
     * @param rootNodeId 根节点的id,空代表查询全部
     * @return Wrapper<List<Node>>
     */
    @RequestMapping(value = "/getCustomerCategoryTree", method = RequestMethod.GET)
    public Wrapper<List<Node>> getCustomerCategoryTree(@RequestParam(value = "rootNodeId", required = false) Long rootNodeId) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerCategoryService.getCustomerCategoryTree(sysUser, rootNodeId));
    }

    /**
     * 查询品类树--供应商
     * @param memberId 供应商id
     * @param memberRoleId 供应商角色id
     * @param rootNodeId 根节点的id,空代表查询全部
     * @param excludeType 排除品类类型 品类类型：1-实物商品、2-虚拟商品、3-服务商品、4-积分兑换商品
     * @return
     */
    @RequestMapping(value = "/getMemberCustomerCategoryTree", method = RequestMethod.GET)
    public Wrapper<List<Node>> getMemberCustomerCategoryTree(@RequestParam(value = "memberId") Long memberId, @RequestParam(value = "memberRoleId") Long memberRoleId, @RequestParam(value = "rootNodeId", required = false) Long rootNodeId,@RequestParam(value = "excludeType",required = false) Integer excludeType) {
        return Wrapper.success(customerCategoryService.getMemberCustomerCategoryTree(rootNodeId, memberId, memberRoleId,excludeType));
    }

    /**
     * 查询品类信息--只有品类信息
     * @param id 品类id
     * @return
     */
    @RequestMapping(value = "/getCustomerCategory", method = RequestMethod.GET)
    public Wrapper<CustomerCategoryResponse> getCustomerCategory(@RequestParam("id") Long id) {
        CustomerCategory customerCategory = customerCategoryService.getCustomerCategory(id);

        if(customerCategory != null){
            CustomerCategoryResponse customerCategoryResponse = this.modelMapper.map(customerCategory, CustomerCategoryResponse.class);
            //将key转成对应的中文名称
            Category category = customerCategory.getCategory();
            if(category != null){
                String fullId = category.getFullId();
                customerCategoryResponse.getCategory().setFullName(categoryService.getFullNameByFullId(fullId));
            }
            return Wrapper.success(customerCategoryResponse);
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 查询品类信息--包括品类、属性、属性值
     * @param id 品类id
     * @return
     */
    @RequestMapping(value = "/getCustomerCategoryById", method = RequestMethod.GET)
    public Wrapper<CustomerCategory> getCustomerCategoryById(@RequestParam("id") Long id) {
        CustomerCategory customerCategory = customerCategoryService.getCustomerCategory(id);
        if(customerCategory != null){
            List<CustomerAttribute> customerAttributeList = customerCategory.getCustomerAttributeList().stream().filter(CustomerAttribute::getIsEnable).collect(Collectors.toList());
            customerAttributeList.forEach(customerAttribute -> {
                List<CustomerAttributeValue> customerAttributeValueList = customerAttribute.getCustomerAttributeValueList().stream().filter(CustomerAttributeValue::getIsEnable).collect(Collectors.toList());
                customerAttribute.setCustomerAttributeValueList(customerAttributeValueList);
            });
            customerCategory.setCustomerAttributeList(customerAttributeList);
            return Wrapper.success(customerCategory);
        }else{
            return null;
        }
    }

    /**
     * 查询品类列表
     * @param pageVO 分页实体
     * @param name 品类名称
     * @return
     */
    @RequestMapping(value = "/getCustomerCategoryList", method = RequestMethod.GET)
    public Wrapper<PageData<CustomerCategoryResponse>> getCustomerCategoryList(PageVO pageVO, @RequestParam("name") String name) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<CustomerCategory> result = customerCategoryService.getCustomerCategoryList(sysUser, pageVO, name);
        List<CustomerCategoryResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<CustomerCategoryResponse>>(){}.getType());
        if(resultList != null && !resultList.isEmpty()){
            resultList.stream().filter(response -> response.getCategory() != null && response.getCategory().getId() != null && response.getCategory().getId() > 0).forEach(response ->
                    response.getCategory().setFullName(categoryService.getFullNameByFullId(response.getFullId()))
            );
        }
        return Wrapper.success(new PageData<>(result.getTotalElements(),resultList));
    }

    /**
     * 新增/修改品类
     * @param customerCategoryRequest 品类实体
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateCustomerCategory", method = RequestMethod.POST)
    public Wrapper<String> saveOrUpdateCustomerCategory(@RequestBody CustomerCategoryRequest customerCategoryRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerCategoryService.saveOrUpdateCustomerCategory(sysUser, this.modelMapper.map(customerCategoryRequest, CustomerCategory.class)));
    }

    /**
     * 删除品类
     * @param simpleIdRequest
     * @return
     */
    @RequestMapping(value = "/deleteCustomerCategory", method = RequestMethod.POST)
    public Wrapper<String> deleteCustomerCategory(@RequestBody SimpleIdRequest simpleIdRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerCategoryService.deleteCustomerCategory(sysUser, simpleIdRequest.getId()));
    }

    /**
     * 通过会员品类id查询会员信息
     */
    @RequestMapping(value = "/getMemberIdList", method = RequestMethod.GET)
    public Wrapper<List<Long>> getMemberIdList(@RequestParam("id") Long id){
        return Wrapper.success(customerCategoryService.getMemberIdList(id));
    }
}
